package com.weng.cloud.service8881.mapStruct.assembler;

import com.weng.cloud.commons.base.TimeUtil;
import com.weng.cloud.service8881.mapStruct.dto.*;
import com.weng.cloud.service8881.mapStruct.other.PageResult;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.Date;

/**
 * @Author: weng
 * @Date: 2022/6/27
 * @Description: com.weng.cloud.service8881.mapStruct.assembler
 */
@Mapper(componentModel = "spring")
public interface SourceDtoAssembler {

    SourceDtoAssembler INSTANCE = Mappers.getMapper(SourceDtoAssembler.class);

    TargetSameDto toTargetSameDto(SourceDto sourceDto);

    @Mappings(
            @Mapping(source = "name",target = "nameCn")
    )
    TargetDiffDto toTargetDiffDto(SourceDto sourceDto);

    TargetExtsDto toTargetExtsDto(SourceDto sourceDto);

    @Mappings({
            @Mapping(target = "date",expression = "java(com.weng.cloud.service8881.mapStruct.assembler.SourceDtoAssembler.convertDate(sourceDto.getDate()))"),
            @Mapping(target = "targetSameDto",expression = "java(com.weng.cloud.service8881.mapStruct.assembler.SourceDtoAssembler.convertTargetSameDto(sourceDto))")
    })
    TargetExpDto toTargetExpDto(SourceDto sourceDto);

    TargetBoxedDto toTargetBoxedDto(SourceDto sourceDto);

    TargetConvertDto toTargetConvertDto(SourceDto sourceDto);

    TargetAuthDto toTargetAuthDto(SourceDto sourceDto);

    @Mappings({
            @Mapping(source = "targetSameDto.id",target = "id"),
            @Mapping(source = "targetAuthDto.name",target = "name"),
            @Mapping(source = "targetAuthDto.ints",target = "ints")
    })
    TargetMultiDto toTargetMultiDto(TargetSameDto targetSameDto,TargetAuthDto targetAuthDto);

//    mapStruct 存在一个泛型的转换问题，如果泛型类的字段存在 来源类不存在的属性，编译将报错
//    PageResult<TargetSameDto> toPagedDtos(PageResult<SourceDto> pagedSourceDtos);

    PageResult<TargetBoxedDto> toPagedDtos(PageResult<SourceDto> pagedSourceDtos);

    static String convertDate(Date date){
        return TimeUtil.toText(date);
    }

    static TargetSameDto convertTargetSameDto(SourceDto sourceDto){
        return TargetSameDto.builder().id(sourceDto.getId()).name(sourceDto.getName()).build();
    }
}
